$(function(){
    var salary_datagrid = $("#salary_datagrid");
    var salary_add_dialog = $("#salary_add_dialog");
    var salary_add_form = $("#salary_add_form");
    var salary_import = $("#salary_import");
    var order = $("#order");
    //排序
    order.combobox({
        onChange:function () {
            salary_datagrid.datagrid("load", {keyword: $("#keyword").val(),
                beginTime: $("#beginTime").val(),endTime: $("#endTime").val(),order:$("#order").val()});
        }
        }
    );
    salary_import.dialog({
        title: '文件上传',
        width: 300,
        height: 130,
        closed: true
    });
    $("#salary_datagrid").datagrid({
        url:'/salary/list',
        fit:true,
        fitColumns:true,
        toolbar:"#salary_buttons",
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'employee',title:'员工',width:100,formatter:formatterEmp},
            {field:'month',title:'月份',width:100},
            {field:'salary',title:'基本工资',width:100},
            {field:'workDays',title:'工作天数',width:100},
            {field:'lateDays',title:'迟到天数',width:100},
            {field:'earlyDays',title:'早退天数',width:100},
            {field:'finalSalary',title:'总工资',width:100},
            {field:'stockDater',title:'结算时间',width:100,formatter:formatterStock}
        ]]
    });
    salary_add_dialog.dialog({
        title: '编辑',
        closed: true,
        buttons:"#salary_add_buttons"
    });
    var cmdObj = {
        searchSal: function () {
            salary_datagrid.datagrid("load", {keyword: $("#keyword").val(),beginTime: $("#beginTime").val(),endTime: $("#endTime").val()});
        },
        import:function () {
            $("#salary_import").dialog("open");
        },
        exlImport:function () {
            salary_import.dialog("open");
        },
        //模板下载
        download:function () {
            window.location.href="/salary/download";
        },
        reloadSal: function () {
            salary_datagrid.datagrid('reload');
        },
        //表格提交/工资表上传
        upload:function () {
            var files = $("#salary_xls").filebox("getValue");
            if (!files) {
                $.messager.alert("温馨提示", "请选择工资表进行上传", "info");
            }else{
                $("#import_form").form("submit", {
                    url: "/salary/importXls",
                    success: function (data) {
                        var data = $.parseJSON(data);
                        if (data.success) {
                           $.messager.alert("温馨提示","导入成功","info",function () {
                               salary_import.dialog("close");
                               salary_datagrid.datagrid("reload");
                           })
                        } else {
                            $.messager.alert("温馨提示", "导入失败!", "error");
                        }
                    }
                });
            }
        },
        delSal: function () {
            var current = salary_datagrid.datagrid("getSelected");
            if (!current) {
                $.messager.alert("提示", "未选中！请选中一行数据后进行操作", "error");
            } else {
                $.messager.confirm("提示", "确定删除该条数据吗？", function (yes) {
                    if (yes) {
                        $.post("/salary/delete", {id: current.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', "操作成功", "info");
                                salary_add_dialog.dialog("close");
                                salary_datagrid.datagrid('reload');
                            } else {
                                $.messager.alert('提示', data.msg, "error");
                            }
                        })
                    }
                })
            }
        },
        eidtSal: function () {
            salary_add_form.form("clear");
            var current = salary_datagrid.datagrid("getSelected");
            if (current) {
                $("#employeeName").html(current.employee.realName);
                current['realName'] = current.employee.realName;
                salary_add_form.form("load", current);
                salary_add_dialog.dialog({
                    title: "编辑",
                    closed: false
                });
            }
            else {
                $.messager.alert("提示", "未选中！请选中一行数据后进行操作", "error");
            }
        },
        accountSal:function(){
            var current = salary_datagrid.datagrid("getSelected");
            if(current.stockDater){
                $.messager.alert("提示",'不能重复核算！','error');
                return;
            }
            if (current) {
                $.messager.confirm("提示", "确定核算该条数据吗？", function (yes) {
                    if (yes) {
                        $.post("/salary/account", {id: current.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', "操作成功", "info");
                                salary_datagrid.datagrid('reload');
                            } else {
                                $.messager.alert('提示', data.msg, "error");
                            }
                        })
                    }
                })
            }
            else {
                $.messager.alert("提示", "未选中！请选中一行数据后进行操作", "error");
            }
        },
        saveSal:function(){
            $("#salary_add_form").form("submit", {
                url: "/salary/update",
                success: function (data){
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功!", "info", function () {
                            salary_add_dialog.dialog("close");
                            salary_datagrid.datagrid("load");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancelSal: function () {
            salary_add_dialog.dialog("close");
        },
        undo:function(){
            salary_import.dialog("close");
        },
        export:function(){
            location.href="/salary/export";
        }
    };
    //绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
});
function formatterEmp(value,row,index){
    if(value){
        return value.realName;
    }
    else{
        return '';
    }
};
function formatterStock(value,row,index){
    if(value){
        return value;
    }
    else{
        return '未结算';
    }
}